﻿using System;
using System.Text;
using Passageways.Logs;
using Passageways.Search;

namespace Passageways.Modules.Rolodex
{
	/// <summary>
	/// Represents the search item that searches contacts.
	/// </summary>
	public partial class SearchContacts : SearchItem
	{
		/// <summary>
		/// Populates the search item with results based on the user's query.
		/// </summary>
		public void Search()
		{
			ContactCollection contacts = Contact.Search(Query, User.ID);
			Results = new SearchResultCollection();

			foreach(Contact contact in contacts)
			{
				StringBuilder description = new StringBuilder();
				description.Append(contact.CompanyName + "<br />");
				description.Append(contact.Email + "<br />");
				description.Append(contact.Address.AddressLine1 + "<br />");

				if(!string.IsNullOrEmpty(contact.Address.AddressLine1))
				{
					description.Append(contact.Address.AddressLine1 + "<br />");
				}

				if(!string.IsNullOrEmpty(contact.Address.AddressLine2))
				{
					description.Append(contact.Address.AddressLine2 + "<br />");
				}

				if(!string.IsNullOrEmpty(contact.Address.City))
				{
					description.Append(contact.Address.City + ", ");
				}

				if(!string.IsNullOrEmpty(contact.Address.State))
				{
					description.Append(contact.Address.State + " ");
				}

				if(!string.IsNullOrEmpty(contact.Address.Zip))
				{
					description.Append(contact.Address.Zip);
				}

				SearchResult result = new SearchResult();
				result.Name = contact.Name;
				result.Description = description.ToString();
				result.HasResourceUrl = false;

				Results.Add(result);
			}
		}
	}
}